// pages/prodetail/prodetail.js
import config from "../../utils/config.js"
import request from "../../utils/request.js"
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    config:[],
    id:[],
    proDesc: [],
    token:"",
    goodId:"",
    goodNum:"",
    proId:"",
    imgIdx:[0],
    normsIdx:[0],
    num: 1,
  },
tohome:function(){
    wx.switchTab({
        url: '/pages/home/home',
    })
},
toshopping:function(){
    wx.switchTab({
        url: '/pages/shopping/shopping',
    })
},
   //添加到购物车
addToShopping:function(){
    var items = this.data.proDesc;
    // console.log(items.info.name);//获取内容
    // console.log(items.info.list_pic_url);//获取图片
    this.removeShop();
    var that = this;
    console.log(that.data.num)
    //添加购物车
    wx.request({
        url: config.addShopCar,
        method:"POST",
        data:{
            goodsId: that.data.goodId ,
            number: that.data.num,
            productId: parseInt(that.data.proId)
            // goodsId: 1156006,
            // number: 100,
            // productId: 1156006

        },
        header:{
            "X-Nideshop-Token": wx.getStorageSync("token")
        },
        success(res){
            console.log(res);
            console.log("123");
        },
        fail(){
          console.log("为获取道数据")
        }
    })
    
  },
  //产品参数弹窗
  toParameter:function(){
    this.parameter = wx.createAnimation({
      duration:800
    })
    this.parameter.bottom(0).step();

    this.setData({
      param: this.parameter.export()
    })
  },
  //移除产品参数弹窗
  remove:function(){
    this.parameter.translateY("100%").step({
      duration: 800
    });
    this.setData({
      param: this.parameter.export()
    })
  },
  // 加入购物车弹窗
  itemsToshop:function(){
    this.GoodsToShop = wx.createAnimation({
      duration:500
    })
    this.GoodsToShop.bottom(0).step();
    this.setData({
      GoodsToShopping: this.GoodsToShop.export()
    })
  },
  //移除购物车弹窗
  removeShop: function () {
    this.GoodsToShop.translateY("100%").step({
      duration: 500
    });
    this.setData({
      GoodsToShopping: this.GoodsToShop.export()
    })
  },
  // 更改购物车中的图片
  cutTab:function(e){
    console.log(e)
    this.setData({
      imgIdx:e.currentTarget.dataset.index
    })
    console.log(this.data)//通过获取imgIdx来获取相关图片
  },
  // 获取产品规格
  normsBtn:function(e){
      console.log(e)
      this.setData({
        normsIdx: e.currentTarget.dataset.index
      })
  },
// 数量加
  addNumber:function(){
    this.data.num++;
    this.setData({
      num:this.data.num
    })
  },
//数量减
  minusNum:function(){
    if(this.data.num > 1){
      this.data.num--;
      this.setData({  
        num:this.data.num
      })
    } else if (this.data.num = 1) {
      wx.showToast({
        title: '商品数量不能为0！',
        icon: 'none',
        duration: 1500//持续的时间
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id:options.id
    })
    console.log(this.data.id);
    var that = this;
      wx.getStorage({
          key: 'token',
          success: function (res) {
              console.log(res.data);
            //   that.setData({
            //       token:res.data
            //   })
              app.globalData.token = res.data;
              console.log(app.globalData.token);
              that.setData({
                  token:res.data
              })
          },
      })
      
  },
  getData: function () {
    var that = this;
    request.request(config.proDesc, 'GET', {id: this.data.id}, function (res) {
      console.log(res.data.data);
      that.setData({
        proDesc: res.data.data,
          goodId: res.data.data.productList["0"].goods_id,
          goodNum: res.data.data.productList["0"].goods_number,
          proId: res.data.data.productList["0"].id
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
      
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      this.getData();
    //   console.log(this.data.token)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
     
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})